package org.blackjuly.sign.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.blackjuly.sign.util.JdbcUtil;

public class DataTrimmer {
	
	private Connection conn;
	
	public DataTrimmer(Connection conn) {
		this.conn = conn;
	}
	
	/**
	 * 查询并返回单条数据
	 */
	public Map<String, Object> selectOne(String sql, Object... params) throws SQLException {
		Map<String, Object> result = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = new HashMap<>();
				ResultSetMetaData rsm = rs.getMetaData();
				for (int i = 0; i < rsm.getColumnCount(); i++) {
					result.put(rsm.getColumnName(i + 1), rs.getObject(i + 1));
				}
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtil.close(pstmt, rs);
		}
		return result;
	}
	
	/**
	 * 查询并返回多条数据
	 */
	public List<Map<String, Object>> selectMore(String sql, Object... params) throws SQLException {
		List<Map<String, Object>> list = new ArrayList<>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Map<String, Object> result = new HashMap<>();
				ResultSetMetaData rsm = rs.getMetaData();
				for (int i = 0; i < rsm.getColumnCount(); i++) {
					result.put(rsm.getColumnName(i + 1), rs.getObject(i + 1));
				}
				list.add(result);
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtil.close(pstmt, rs);
		}
		return list;
	}
	
	/**
	 * 执行更新语句
	 */
	public int executeUpdate(String sql, Object... params) throws SQLException {
		int result = 0;
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtil.close(pstmt);
		}
		return result;
	}
	
	/**
	 * 执行sql语句
	 */
	public boolean execute(String sql, Object... params) throws SQLException {
		boolean result = false;
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			result = pstmt.execute();
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtil.close(pstmt);
		}
		return result;
	}
	
	public boolean hasMoreData(String sql, Object... params) throws SQLException {
		boolean result = false;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			rs = pstmt.executeQuery();
			if (rs.next()) {
				result = true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			JdbcUtil.close(pstmt, rs);
		}
		return result;
	}
	
}